package com.moonshineproject.plugin.actionscript.swflauncher
{
	import com.moonshineproject.events.FilePluginEvent;
	import com.moonshineproject.plugin.PluginBase;
	import com.moonshineproject.plugin.actionscript.as3project.vo.AS3ProjectVO;
	import com.moonshineproject.plugin.actionscript.mxmlc.MXMLCPlugin;
	import com.moonshineproject.plugin.actionscript.swflauncher.event.SWFLaunchEvent;
	import com.moonshineproject.plugin.project.vo.ProjectVO;
	import com.moonshineproject.plugin.settings.event.RequestSettingEvent;
	
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.filesystem.File;

	public class SWFLauncherPlugin extends PluginBase
	{	
		override public function get name():String			{ return "SWF Launcher Plugin"; }
		override public function get author():String		{ return "Moonshine Project Team"; }
		override public function get description():String	{ return "Opens .swf files externally. Handles AIR launching via ADL."; }

		override public function activate():void 
		{
			super.activate();
			dispatcher.addEventListener(SWFLaunchEvent.EVENT_LAUNCH_SWF, launchSwf);
			dispatcher.addEventListener(FilePluginEvent.EVENT_FILE_OPEN, handleOpenFile);
		}
		
		override public function deactivate():void 
		{
			super.deactivate();
			dispatcher.removeEventListener(SWFLaunchEvent.EVENT_LAUNCH_SWF, launchSwf);
			dispatcher.removeEventListener(FilePluginEvent.EVENT_FILE_OPEN, handleOpenFile);
		}
		
		protected function handleOpenFile(event:FilePluginEvent):void
		{
			if (event.file.extension == "swf")
			{
				// Stop Moonshine from trying to open this file
				event.preventDefault();
				// Fake event
				launchSwf(
					new SWFLaunchEvent(SWFLaunchEvent.EVENT_LAUNCH_SWF, event.file)
				);
			}
		}
		
		protected function launchSwf(event:SWFLaunchEvent):void
		{
			// Find project if we can (otherwise we can't open AIR swfs)
			if (!event.project) event.project = findProjectForFile(event.file);
			
			// Do we have an AIR project on our hands?
			if (event.project is AS3ProjectVO
				&& AS3ProjectVO(event.project).testMovie == AS3ProjectVO.TEST_MOVIE_AIR)
			{
				launchAIR(event.file, AS3ProjectVO(event.project), event.sdk);
			}
			else
			{
				// Open with default app
				launchExternal(event.file);
			}
		}
		
		protected function findProjectForFile(file:File):ProjectVO
		{
			for each (var project:ProjectVO in model.projects)
			{
				// See if we're part of this project
				if (file.nativePath.indexOf(project.folder.nativePath) == 0)
				{
					return project;
				}
			}
			return null;
		}
		
		protected function launchAIR(file:File, project:AS3ProjectVO, sdk:File):void
		{
			// Can't open files without an SDK set
			if (!sdk && !project.buildOptions.customSDK)
			{
				// Try to fetch default value from MXMLC plugin
				var event:RequestSettingEvent = new RequestSettingEvent(MXMLCPlugin, 'defaultFlexSDK');
				dispatcher.dispatchEvent(event);
				// None found, abort
				if (event.value == "" || event.value == null) return;
				
				// Default SDK found, let's use that
				sdk = new File(event.value.toString());
			}
			
			// Need project opened to run
			if (!project) return;
			
			var currentSDK:File = (project.buildOptions.customSDK) ? project.buildOptions.customSDK : sdk;
			
			var customInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
			// Find air debug launcher
			var executable:File = currentSDK.resolvePath("bin/adl");
			customInfo.executable = executable;
			// Guesstimate app-xml name
			var appXML:String = "src/"+project.projectName+"-app.xml";
			var rootPath:String = project.folder.getRelativePath(file.parent);

			customInfo.arguments = Vector.<String>([appXML, rootPath]);
			customInfo.workingDirectory = project.folder;

			var customProcess:NativeProcess = new NativeProcess();
			customProcess.start(customInfo);
		}
		
		protected function launchExternal(file:File):void
		{
			// Start with systems default handler for .swf filetype
			file.openWithDefaultApplication();
		}
		
	}
}